<?php
/**
 * Created by JetBrains PhpStorm.
 * User: zhouyang
 * Date: 13-7-15
 * Time: 下午4:31
 * To change this template use File | Settings | File Templates.
 */

class UploadAction extends Action{

    private $upload;

    /**
     * 构造方法
     * 初始化
     */
    public function __construct(){
		$this->upload = new Upload();
    }

    public function uploadCommon(){
        $upload = new Upload();
        $upload->maxSize  = 10485760 ;
        //设置需要生成缩略图，仅对图像文件有效
        $upload->thumb = true;
        //设置需要生成缩略图的文件后缀
        $upload->thumbPrefix = '';  //生产2张缩略图
        //设置缩略图最大宽度
        $upload->thumbMaxWidth = '320';
        //设置缩略图最大高度
        $upload->thumbMaxHeight = '200';

        //替换thumb图片
        if($_POST['curr_thumb']){
            $curr_thumb = explode('/',$_POST['curr_thumb']);
            $thumb_key = count($curr_thumb) -1;
            $curr_thumb1 = explode('.',$curr_thumb[$thumb_key]);

            $thumb_basename = $curr_thumb1[0];
            $upload->thumbFile = $thumb_basename;
            $thumb_extension = $curr_thumb1[1];
            $upload->thumbExt = $thumb_extension;

            $dir =  './';
            foreach($curr_thumb as $k => $v){
                if($thumb_key != $k){
                    $dir .= $v . '/';
                }
            }

            $upload->savePath =  str_replace('thumb/','',$dir);
            //设置缩略图路径
            $upload->thumbPath =  $dir;
        }else{
            $upload->thumbFile = date('YmdHis') . '_' . rand(1,999999999);
            $dir =  './Uploads/image/'.date('Ymd').'/';
            $upload->savePath = $dir;
            //设置缩略图路径
            $upload->thumbPath =  $dir.'thumb/';
        }
        //删除原图
        $upload->thumbRemoveOrigin = true;
        if(!$upload->upload()) {
            $this->error($upload->getErrorMsg());
        }else{
            $info =  $upload->getUploadFileInfo();

            //echo  $info[0]['savename'];
            $thumb_ext = pathinfo($info[0]['savename']);
            echo $upload->thumbPath.$upload->thumbFile.'.'.$thumb_ext['extension'];
        }
    }

    /**
     * 新闻 缩图 上传
     */
    public function newPictureUpload(){
        $this->upload->maxSize  = 10485760 ;
        //设置需要生成缩略图，仅对图像文件有效
        $this->upload->thumb = true;
        //设置需要生成缩略图的文件后缀
        $this->upload->thumbPrefix = '_thumb';  //生产2张缩略图
        //设置缩略图最大宽度
        $this->upload->thumbMaxWidth = '320';
        //设置缩略图最大高度
        $this->upload->thumbMaxHeight = '200';

        $curr_picture = $this->input()->post('curr_picture');

        if($curr_picture){
            $curr_path = explode('/',$curr_picture);
            $thumb_key = count($curr_path) -1;
            $picture_info = explode('.',$curr_path[$thumb_key]);

            $thumb_basename = $picture_info[0];
            $this->upload->thumbFile = $thumb_basename;
            $thumb_extension = $picture_info[1];
            $this->upload->thumbExt = $thumb_extension;

            $dir =  './';
            foreach($curr_path as $k => $v){
                if($thumb_key != $k){
                    $dir .= $v . '/';
                }
            }
            //上传图片路径
            $this->upload->savePath =  str_replace('thumb','temp',$dir);
            //设置缩略图路径
            $this->upload->thumbPath =  $dir;
        }else{
            $this->upload->thumbFile = date('YmdHis') . '_' . rand(1,999999999);
            $dir =  './uploads/news/temp/'.date('Y').'/'.date('m').'/'.date('d').'/';
            //上传图片路径
            $this->upload->savePath = $dir;
            //设置缩略图路径
            $this->upload->thumbPath =  str_replace('temp', 'thumb', $dir);
        }
        //删除原图
        $this->upload->thumbRemoveOrigin = true;
        if(!$this->upload->uploadSingle()) {
            $this->error($this->upload->getErrorMsg());
        }else{
            $info =  $this->upload->getUploadFileInfo();

            //echo  $info[0]['savename'];
            $thumb_ext = pathinfo($info[0]['savename']);
            echo $this->upload->thumbPath.$this->upload->thumbFile.'.'.$thumb_ext['extension'];
        }
	}

    /**
     * 新闻 内容图片 上传
     */
    public function newContentUpload(){
	
	}
}